package com.csproj.dispatch.service;

import com.csproj.basic.repository.ContainerRepository;
import com.csproj.dispatch.domain.Logistics;
import com.csproj.dispatch.repository.DispatchRepository;
import com.csproj.dispatch.repository.LogisticsRepository;
import com.csproj.dispatch.service.dto.LogisticsDTO;
import com.csproj.dispatch.service.mapper.LogisticsMapper;
import com.csproj.siiat.base.common.dto.BaseDTO;
import com.csproj.siiat.security.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing Logistics.
 * 物流信息-业务
 */
@Service
@Transactional
public class LogisticsService {

    private final Logger log = LoggerFactory.getLogger(LogisticsService.class);

    private final LogisticsRepository logisticsRepository;

    private final DispatchRepository dispatchRepository;

    private final ContainerRepository containerRepository;

    private final LogisticsMapper logisticsMapper;

    public LogisticsService(LogisticsRepository logisticsRepository, LogisticsMapper logisticsMapper,
                            DispatchRepository dispatchRepository, ContainerRepository containerRepository) {
        this.logisticsRepository = logisticsRepository;
        this.logisticsMapper = logisticsMapper;
        this.dispatchRepository = dispatchRepository;
        this.containerRepository = containerRepository;
    }

    /**
     * 保存物流信息
     * @param logisticsDTO
     * @return
     */
    public LogisticsDTO save(LogisticsDTO logisticsDTO) {
        log.debug("Request to save Logistics : {}", logisticsDTO);
        Logistics logistics = logisticsMapper.toEntity(logisticsDTO);

        // 设置创建人、创建时间
        logistics.setCreationTime(ZonedDateTime.now());
        logistics.setCreatorUserId(SecurityUtils.getCurrentUserId());
        logistics = logisticsRepository.save(logistics);
        return logisticsMapper.toDto(logistics);
    }

    /**
     * Get all the logistics.
     *
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<LogisticsDTO> findAll() {
        log.debug("Request to get all Logistics");
        return logisticsRepository.findAll().stream()
            .map(logisticsMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * 通过集装箱主键获取物流信息
     * @param containerid
     * @return
     */
    @Transactional(readOnly = true)
    public LogisticsDTO findLogisticsByContainerid(String containerid) {
        log.debug("Request to get one Logistics by containerid : {}", containerid);
        return  logisticsMapper.toDto(logisticsRepository.findFirstByContainerIdAndDeletionTimeIsNullOrderByCreationTimeDesc(containerid));
    }

    /**
     * 通过物流信息id更新物流信息
     * @param id
     * @param logisticsDTO
     * @return
     */
    public LogisticsDTO updateLogisticsById(String id, LogisticsDTO logisticsDTO) {
        LogisticsDTO logisticsDTO1 = null;
        if(id != null && !"".equals(id)){
            Optional<Logistics> optional = Optional.ofNullable(logisticsRepository.findOne(id));
            if(optional.isPresent()){
                Logistics logistics = logisticsMapper.toEntity(logisticsDTO);
                logistics.setId(id);

                // 设置更新时间、更新人
                logistics.setLastModificationTime(ZonedDateTime.now());
                logistics.setLastModifierUserId(SecurityUtils.getCurrentUserId());
                logistics = logisticsRepository.save(logistics);
                return (logisticsDTO1 = logisticsMapper.toDto(logistics));
            }
        }
        return logisticsDTO1;
    }

    public BaseDTO deleteLogisticsById(String id) {
        if(id != null && !"".equals(id)){
            //TODO 此处使用硬删除还是软删除？
            Optional<Logistics> optional = Optional.ofNullable(logisticsRepository.findOne(id));
            if(optional.isPresent()){
                Logistics logistics = optional.get();
                logistics.setDeletedFlag(true);
                logistics.setDeletionTime(ZonedDateTime.now());
                // 删除人
                logistics.setDeleterUserId(SecurityUtils.getCurrentUserId());
                logistics.setLastModifierUserId(SecurityUtils.getCurrentUserId());
                logistics.setLastModificationTime(ZonedDateTime.now());
                logistics = logisticsRepository.save(logistics);
                return BaseDTO.createBaseDTO();
            }
            //此处硬删除
//            logisticsRepository.delete(id);
//            return BaseDTO.createBaseDTO();
        }
        return new BaseDTO(BaseDTO.CODE_PARAM, "物流信息主键不能为空", null);
    }




}
